
var mouseX = 0;
var mouseY = 0;
document.addEventListener('mousemove', function (event) {
    mouseX = event.clientX;
    mouseY = event.clientY;
});


if (window.neutron_winid != undefined) {
    var winapi_callback_lst = { 0: (() => { }) }
    var winapi_callback_lst_num = 0;
    var neutron = {
        "winid": window.neutron_winid,
        "setMinSize": function (minw, minh) {
            parent.postMessage({
                "from": "neutron_panel",
                "type": "setMinSize",
                "winid": window.neutron_winid,
                "datas": [
                    minw, minh
                ]
            }, "*");
        },
        "setWinSize": function (w, h) {
            parent.postMessage({
                "from": "neutron_panel",
                "type": "setWinSize",
                "winid": window.neutron_winid,
                "datas": [
                    w, h
                ]
            }, "*");
        },
        "setTitle": function (title) {
            parent.postMessage({
                "from": "neutron_panel",
                "type": "setTitle",
                "winid": window.neutron_winid,
                "datas": [
                    title
                ]
            }, "*");
        },
        "setPosition": function (x, y) {
            parent.postMessage({
                "from": "neutron_panel",
                "type": "setPosition",
                "winid": window.neutron_winid,
                "datas": [
                    x, y
                ]
            }, "*");
        },
        "createWin": function (title = "New Window", content = "", w = 200, h = 200, callback = (() => { })) {
            parent.postMessage({
                "from": "neutron_panel",
                "type": "createWin",
                "winid": window.neutron_winid,
                "datas": [
                    w, h, title, content, 0
                ]
            }, "*");
        },
        "createIframeWin": function (title = "New Window", link = "/login", w = 200, h = 200, callback = (() => { })) {
            var content = "<iframe class=\"win_window-iframe\" src=\"" + link + "\">Loading...</iframe>"
            winapi_callback_lst[++winapi_callback_lst_num] = callback
            parent.postMessage({
                "from": "neutron_panel",
                "type": "createWin",
                "winid": window.neutron_winid,
                "datas": [
                    w, h, title, content, winapi_callback_lst_num
                ]
            }, "*");
        },
        "sendMessageToWin": function (winid, message) {
            parent.postMessage({
                "from": "neutron_panel",
                "type": "sendMessageToWin",
                "winid": window.neutron_winid,
                "datas": [
                    winid, message
                ]
            }, "*");
        },
        "saveData": function (datas) {
            parent.postMessage({
                "from": "neutron_panel",
                "type": "saveData",
                "winid": window.neutron_winid,
                "datas": [
                    datas
                ]
            }, "*");
        },
        "loadData": function (callback = (() => { })) {
            winapi_callback_lst[++winapi_callback_lst_num] = callback
            parent.postMessage({
                "from": "neutron_panel",
                "type": "loadData",
                "winid": window.neutron_winid,
                "datas": [
                    winapi_callback_lst_num
                ]
            }, "*");
        },
        "getInfo": function (callback = ((data) => { })) {
            winapi_callback_lst[++winapi_callback_lst_num] = callback
            parent.postMessage({
                "from": "neutron_panel",
                "type": "getInfo",
                "winid": window.neutron_winid,
                "datas": [
                    winapi_callback_lst_num
                ]
            }, "*");
        },
        "onClose": function (callback = (() => { })) {
            winapi_callback_lst[++winapi_callback_lst_num] = callback
            parent.postMessage({
                "from": "neutron_panel",
                "type": "onClose",
                "winid": window.neutron_winid,
                "datas": [
                    winapi_callback_lst_num
                ]
            }, "*");
        },
        "close": function () {
            parent.postMessage({
                "from": "neutron_panel",
                "type": "close",
                "winid": window.neutron_winid,
                "datas": [
                ]
            }, "*");
        },
        "showContextMenu": function (callback = (() => { }), file_list, x, y) {
            winapi_callback_lst[++winapi_callback_lst_num] = callback
            if (x == null) {
                parent.postMessage({
                    "from": "neutron_panel",
                    "type": "context",
                    "winid": window.neutron_winid,
                    "datas": [
                        winapi_callback_lst_num,
                        file_list,
                        mouseX, mouseY
                    ]
                }, "*");
            } else {
                parent.postMessage({
                    "from": "neutron_panel",
                    "type": "context",
                    "winid": window.neutron_winid,
                    "datas": [
                        winapi_callback_lst_num,
                        file_list,
                        x, y
                    ]
                }, "*");
            }
        },
    }
    window.addEventListener("message", winapi_receiveMessage, false);
    function winapi_receiveMessage(event) {
        if (event.origin != location.origin) {
            return;
        }
        var data = event.data;
        if (data.from != "neutron_panel_callback") {
            return;
        }
        if (data.callback_id != 0) {
            winapi_callback_lst[data.callback_id](data.datas);
            delete winapi_callback_lst[data.callback_id];
        }
    }
    function alert(msg) {
        var elem = document.createElement("html")
        elem.appendChild(document.createElement("head")).appendChild(document.createElement("script")).innerHTML = "window.neutron_winid=" + 0
        var body = elem.appendChild(document.createElement("body")).appendChild(document.createElement("div"))
        body.innerText = msg
        body.style.padding = "10px"
        body.style.textAlign = "center"
        body.style.fontSize = "18px"
        neutron.createWin("Alert", elem.innerHTML, 300, 120)
    }
} else {
    function random(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    window.neutron_winid = random(100000, 999999)
    document.head.appendChild(document.createElement("script")).src = "/static/js/desktop_menu.js";
    var cssobj = document.createElement("link")
    cssobj.href = "/static/css/desktop_menu.css";
    cssobj.rel = "stylesheet";
    document.head.appendChild(cssobj)
    var neutron = {
        "winid": window.neutron_winid,
        "setMinSize": function (minw, minh) {
        },
        "setWinSize": function (w, h) {
            window.resizeTo(w, h)
        },
        "setTitle": function (title) {
            document.title = title;
        },
        "setPosition": function (x, y) {
            window.moveTo(x, y)
        },
        "createWin": function (title = "New Window", content = "", w = 200, h = 200) {
            var open_win = window.open("about:blank", title, "location=no,width=" + w + ",height=" + h)
            open_win.document.write(content)
        },
        "createIframeWin": function (title = "New Window", link = "/login", w = 200, h = 200, callback = (() => { })) {
            window.open(link, title, "location=no,width=" + w + ",height=" + h);
        },
        "sendMessageToWin": function (winid, message) {
            window.opener.postMessage(
                message
                , "*");
        },
        "saveData": function (datas) {
            localStorage.setItem('data' + location.pathname, JSON.stringify(datas))
        },
        "loadData": function (callback = (() => { })) {
            var ret = JSON.parse(localStorage.getItem('data' + location.pathname))
            if (ret == undefined) {
                ret = null
            }
            callback([ret])
        },
        "getInfo": function (callback = ((data) => { })) {
            callback([
                {
                    "x": 0,
                    "y": 0,
                    "width": document.documentElement.clientWidth,
                    "height": document.documentElement.clientHeight,
                }
            ], "*");
        },
        "onClose": function (callback = (() => { })) {
            window.onbeforeunload = callback
        },
        "close": function () {
            window.close();
        },
        "showContextMenu": function (callback = (() => { }), file_list, x, y) {
            menu_callback = (ret) => { callback([ret]) }
            create_menu_on_mouse(file_list)
        },
    }
}